package cn.engage.ekcc.service.impl;

import cn.engage.ekcc.domain.Product;
import cn.engage.ekcc.domain.ProductBuy;
import cn.engage.ekcc.domain.ProductLog;
import cn.engage.ekcc.exception.GobalException;
import cn.engage.ekcc.fegincliect.BuyFeginCliect;
import cn.engage.ekcc.fegincliect.SellFeginCliect;
import cn.engage.ekcc.mapper.ProductMapper;
import cn.engage.ekcc.service.IProductService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author director
 * @since 2022-11-18
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    /* 低于20添加采购表和日志表 */
    @Value("${inventory.text}")
    private Long number;
    /* 低于10就是缺货 */
    @Value("${inventory.name}")
    private Long getNumber;
    @Autowired
    private SellFeginCliect sellFeginCliect;
    @Autowired
    private BuyFeginCliect buyFeginCliect;
    /**
     * 商品出库
     * @param
     */
    @Override
    public void CK(Product product) {
        Product product1 = productMapper.selectById(product.getId());
        if (product1 == null){
            throw new GobalException("该商品不存在","-11");
        }
        if (product1.getProductNumber() < product.getProductNumber()){
            throw new GobalException("库存不足");
        }
        product1.setProductNumber(product1.getProductNumber() - product.getProductNumber());
        productMapper.updateById(product1);
        if ((product1.getProductNumber()) <= number){
            // 库存不足调用sell保存日志
            ProductLog productLog = new ProductLog();
            productLog.setProductId(product1.getId());
            productLog.setLog(product1.getName() + "库存不足请及时补充");
            sellFeginCliect.saveOrUpdate(productLog);
            // 库存不足调用采购服务进行采购
            ProductBuy productBuy = new ProductBuy();
            productBuy.setProductId(product1.getId());
            // 0为待审核
            productBuy.setType(0);
            productBuy.setNote(product1.getName() + "库存不足请及时补充");
            buyFeginCliect.saveOrUpdate(productBuy);
        }
    }
    /* 查询缺货产品 */
    @Override
    public List<Product> selectQH() {
       List<Product> list = new ArrayList<>();
        List<Product> products = productMapper.selectList(null);
        products.forEach(e ->{
            /* 低于10就是缺货 */
            if (e.getProductNumber() <= getNumber){
                list.add(e);
            }
        });
        return list;
    }
    /* 商品入库 */
    @Override
    public void RK(Product product) {
        Product product1 = productMapper.selectById(product.getId());
        if (product1 == null){
            throw new GobalException("该商品不存在");
        }
        product1.setProductNumber(product1.getProductNumber() + product.getProductNumber());
        productMapper.updateById(product1);
    }
}
